//
//  TMGEarningsDetailViewController.m
//  TMGold
//
//  Created by Mac on 2018/3/19.
//  Copyright © 2018年 tangmu. All rights reserved.
//

#import "TMGEarningsDetailViewController.h"
#import "TMGEarningsTableViewCell.h"
@interface TMGEarningsDetailViewController ()
@property(nonatomic, strong) NSString                     *money;
@property(nonatomic, strong) NSMutableArray <TMGEarningModel*>                    *dataSouce;
@end

@implementation TMGEarningsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"累计收益";

    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无收益" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendEaringRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 60)];
    header.backgroundColor = [UIColor whiteColor];
    

    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(14, 5, 200, 20)
                                                     text:[NSString stringWithFormat:@"累计收益(元)"]
                                                textColor:FirstTextColor
                                                     font:16
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    
    

    
    STLabel * moneyLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 60)
                                                      text:[NSString stringWithFormat:@"￥%@",self.money]
                                                 textColor:TM_orangeColor
                                                      font:15
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentCenter];
    [header addSubview:moneyLable];
    
    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];

}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGEarningsTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGEarningsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGEarningsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    TMGEarningModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendEaringRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/detail/getIncomeDetails"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"][@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSString * allEning = responseObject[@"data"][@"total_money"];
                                                 self.money = allEning;
                                                 if (array.count) {
                                                      [self configSubView];
                                                 }
                                               
                                                 for (NSDictionary *modelDic in array) {
                                                     TMGEarningModel * model = [TMGEarningModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
